﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace DevDemo
{
    public partial class DevGridControlRowStyle : DevExpress.XtraEditors.XtraForm
    {
        public DevGridControlRowStyle()
        {
            InitializeComponent();
        }

        private void DevGridControlRowStyle_Load(object sender, EventArgs e)
        {
            //List<object> list = new List<object>();
            //list.Add(new { BgColor = 11, BgColor2 = 12, FontColor = 13 });
            //list.Add(new { BgColor = 21, BgColor2 = 22, FontColor = 23 });
            //list.Add(new { BgColor = 31, BgColor2 = 32, FontColor = 33 });
            //gridControl1.DataSource = list;

            DataTable dt = new DataTable();

            DataColumn dc1 = new DataColumn();
            dc1.ColumnName = "BgColor";
            dt.Columns.Add(dc1);
            DataColumn dc2 = new DataColumn();
            dc2.ColumnName = "BgColor2";
            dt.Columns.Add(dc2);
            DataColumn dc3 = new DataColumn();
            dc3.ColumnName = "FontColor";
            dt.Columns.Add(dc3);

            DataRow dr = dt.NewRow();
            dr["BgColor"] = "11";
            dr["BgColor2"] = "12";
            dr["FontColor"] = "13";
            dt.Rows.Add(dr);

            DataRow dr2 = dt.NewRow();
            dr2["BgColor"] = "21";
            dr2["BgColor2"] = "22";
            dr2["FontColor"] = "23";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3["BgColor"] = "31";
            dr3["BgColor2"] = "32";
            dr3["FontColor"] = "33";
            dt.Rows.Add(dr3);

            gridControl1.DataSource = dt;
        }

        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            //GridView View = sender as GridView;

            //if (e.Column.FieldName == "BgColor")//设背景
            //{
            //    //int pointID = (gridView1.GetRow(e.RowHandle) as object).BgColor;
            //    e.Appearance.BackColor = Color.Red;
            //}
            //else if (e.Column.FieldName == "BgColor2")//设渐变背景
            //{
            //    //int pointID = (gridView1.GetRow(e.RowHandle) as object).BgColor;
            //    e.Appearance.BackColor = Color.Red;
            //    e.Appearance.BackColor2 = Color.FromArgb(255, 255, 255);
            //}
            //else if (e.Column.FieldName == "FontColor")//设字体颜色
            //{
            //    //int pointID = (gridView1.GetRow(e.RowHandle) as object).BgColor;
            //    e.Appearance.ForeColor = Color.Red;//Color.FromArgb(42, 173, 231);
            //}
        }

        private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView a = gridView1;

            int hand = e.RowHandle;//行号

            if (hand < 0)
            {
                return;
            }

            DataRow dr = gridView1.GetDataRow(hand);

            if (dr == null)
            {
                return;
            }

            string str = gridView1.GetRowCellValue(hand, "BgColor2").ToString();

            if (str == "22")
            {
                e.Appearance.ForeColor = Color.Red;//字体颜色

                e.Appearance.BackColor = Color.Linen;//行背景颜色
            }
        }
    }
}